package com.zx.练习题._2020面试题;

import java.util.HashSet;
import java.util.Set;

public class CP2 {

    //2、两个数组，a=[1,2,3,4,5] ,  b=[1,2, 4,5] ，a中有一个元素是b中没有的，且两个数组是顺序递增的。求这个值
    public static void main(String[] args) {
        int[] a = {1, 3, 5};
        //b中没有，不表示b是a的子集
        int[] b = {1, 2, 3, 4};

        impl1(new int[]{1, 3, 5}, new int[]{1, 2, 3, 4});
        impl1(new int[]{1, 3, 5}, new int[]{1, 2, 4, 5});
        impl1(new int[]{1, 3, 5}, new int[]{2, 3, 4, 5});
        System.out.println("############");
        impl2(new int[]{1, 3, 5}, new int[]{1, 2, 3, 4});
        impl2(new int[]{1, 3, 5}, new int[]{1, 2, 4, 5});
        impl2(new int[]{1, 3, 5}, new int[]{2, 3, 4, 5});
    }

    private static void impl1(int[] a, int[] b) {
        Set<Integer> set = new HashSet<>();
        for (int i : b) {
            set.add(i);
        }

        for (int i : a) {
            if (!set.contains(i)) {
                System.out.println(i);
                break;
            }
        }
    }

    private static void impl2(int[] a, int[] b) {
        int i = 0, j = 0;

        int res = -1;
        while (i < a.length && j < b.length) {
            if (a[i] == b[j]) {
                i++;
                j++;
            } else if (a[i] < b[j]) {
                res = a[i];
                break;
            } else {
                j++;
            }
        }

        if (res == -1 && i < a.length) {
            res = a[i];
        }

        System.out.println(res);
    }
}
